package com.wxproject.demo.Controller.Socket;

import com.wxproject.demo.Common.lang.ResultBody;
import com.wxproject.demo.Entity.AO.ChatMessageAO;
import com.wxproject.demo.Entity.AO.ChatUserAO;
import com.wxproject.demo.Entity.AO.SenderAO;
import com.wxproject.demo.Entity.VO.AllUserInfo;
import com.wxproject.demo.Entity.VO.ChatMessageVO;
import com.wxproject.demo.Entity.VO.ChatRoomRecord;
import com.wxproject.demo.Entity.VO.RoomVO;
import com.wxproject.demo.Service.ChatService;
import com.wxproject.demo.Service.RoleService;
import com.wxproject.demo.Service.UserInfoService;
import com.wxproject.demo.Service.UserService;
import com.wxproject.demo.Socket.WebSocket;
import com.wxproject.demo.Utils.JWTUtil;
import com.wxproject.demo.Utils.RedisUtil;
import com.wxproject.demo.Utils.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zqf
 * @date 2021/4/5 22:58
 * @description
 */
@RestController
public class SocketController {
    @Autowired
    private WebSocket webSocket;
    @Autowired
    private RoleService userMapper;
    @Autowired
    private ChatService chatService;
    @Autowired
    private UserInfoService userInfoService;

    @GetMapping("/getSocketRoom")
    public ResultBody getSocketRoom(){
        List<RoomVO> roomVOS = chatService.getSocketRoom();
        return ResultBody.ok().data("room",roomVOS);
    }

    //向某个聊天室的所有用户发送信息
    @PostMapping("/sendAllWebSocket")
    public ResultBody sendAllWebSocket(@RequestBody @Validated ChatMessageAO chatMessageAO, HttpServletRequest request) {
//        String roomId = chatService.getRoomIdByTypeId(chatMessageAO.getTypeId());
        String roomId =chatMessageAO.getRoomId();
        String username = JWTUtil.getUsername(request.getHeader("Token"));
//        String roomName = chatService.getRoomNameByTypeId(chatMessageAO.getTypeId());
        String roomName = chatService.getRoomNameByRoomId(roomId);
        chatMessageAO.setUsername(username);
        chatMessageAO.setId(String.valueOf(SnowFlake.nextId()));
        chatMessageAO.setRoomId(roomId);
        AllUserInfo userInfo = userInfoService.getAllUserInfo(JWTUtil.getUserId(request.getHeader("Token")));
        webSocket.sendAllMessage(chatMessageAO.getMessage(),roomName,JWTUtil.getUsername(request.getHeader("Token")),userInfo.getAvatarUrl());
        if (chatService.saveChatRoomRecord(chatMessageAO)!=0){
            return ResultBody.ok().message("发送成功");
        }
            return ResultBody.error().message("发送失败");
     }


    //向某个用户发送信息
    @PostMapping("/sendOneWebSocket")
    public ResultBody sendOneWebSocket(@RequestBody SenderAO senderAO, HttpServletRequest request) {
        String token = request.getHeader("Token");
        String senderName = JWTUtil.getUsername(token);
        String receiverName = senderAO.getReceiverName();
        senderAO.setSenderName(senderName);
        senderAO.setId(String.valueOf(SnowFlake.nextId()));
        webSocket.sendOneMessage(senderName,receiverName,senderAO.getMessage());
        if (chatService.saveMessage(senderAO)!=0){
            return ResultBody.ok().message("发送成功");
        }
        return ResultBody.error().message("发送失败");
    }

    //根据自己的用户名和对方的用户名，查找私聊记录
    @GetMapping("/getPrivateRecord")
    public ResultBody getMessageRecord(@RequestParam("otherName")String otherName,HttpServletRequest request){
        String myName = JWTUtil.getUsername(request.getHeader("Token"));
        ChatUserAO chatUserAO = new ChatUserAO();
        chatUserAO.setOtherName(otherName);
        chatUserAO.setMyName(myName);
        List<ChatMessageVO> res = chatService.getChatMessage(chatUserAO);
        return ResultBody.ok().data("messageRecord",res);
    }

    //根据聊天室ID查询聊天室聊天记录
    @GetMapping("/getChatRoomRecord")
    public ResultBody getChatRoomRecord(@RequestParam("roomId")String roomId){
//        String roomId = chatService.getRoomIdByTypeId(typeId);
        List<ChatRoomRecord> res = chatService.getChatRoomRecord(roomId);
        return ResultBody.ok().data("roomRecord",res);
    }
}
